package com.jicheng.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jicheng.Utils.ResultMess;
import com.jicheng.dto.integrity.ReportDto;
import com.jicheng.dto.integrity.ReportPagingParams;
import com.jicheng.dto.integrity.ReportParams;
import com.jicheng.dto.user.SuperviseUserParams;
import com.jicheng.dto.user.UserDto;
import com.jicheng.dto.user.UserPagingParams;
import com.jicheng.ext.dto.PagingResult;
import com.jicheng.service.IDataSyncService;
import com.jicheng.service.IIntegrityService;
import com.jicheng.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "user-management（终端用户管理）")
@RestController
@RequestMapping(value = "user-management", produces = "application/json;charset=UTF-8")
public class AppUserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IIntegrityService integrityService;
    @Autowired
    private IDataSyncService dataSyncService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation(value = "创建用户信息")
    public ResultMess save(@RequestBody SuperviseUserParams params) {
       int size =  userService.exist(params.getAppId());
       if(size>0){
           userService.update(params);
           return new ResultMess(true,"修改成功！",null);
       }else{

           userService.create(params);
           return new ResultMess(true,"创建成功！",null);
       }


    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新用户信息")
    public ResultMess update(SuperviseUserParams params, MultipartFile file) {
        if(file!=null){
            String imgPath = dataSyncService.syncPhotos(file);
            params.setHeader(imgPath);
        }
        userService.update(params);
        return new ResultMess(true,"更新成功！",null);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户列表")
    public PagingResult<UserDto> getList( UserPagingParams params) {
        List<UserDto> list = userService.getList(params);
        PageHelper.startPage(params.getPage(), params.getPageSize());
        PageInfo<UserDto> userDtoPageInfo = new PageInfo<>(list);
        PagingResult<UserDto> result = new PagingResult();
        result.setList(userDtoPageInfo.getList());
        result.setTotal(userDtoPageInfo.getTotal());
        return result ;
    }

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据用户id获取用户信息（可以用于同步用户数据）")
    public UserDto getList(@PathVariable("id")@ApiParam("用户id")String id) {
        return userService.getById(id);
    }





    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "增加期数")
    public void save(@RequestBody ReportParams params) {
            integrityService.saveReport(params);
    }

    @RequestMapping(value = "/periodList", method = RequestMethod.GET)
    @ApiOperation(value = "获取期数列表")
    public ResultMess<PagingResult<ReportDto>> getList(ReportPagingParams params) {
        PagingResult<ReportDto> list = integrityService.getList(params);
        return new ResultMess<>(true,"",list);
    }

}
